﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Semantics;
namespace WebParser
{
    public partial class _Default : System.Web.UI.Page
    {
        SortedList<string, SortedList<string, LanguageNode>> dictionary = new SortedList<string, SortedList<string, LanguageNode>>();
        List<LanguageNode> langnodes;

        protected void Page_Load(object sender, EventArgs e)
        {
            //this.LabelError.Text = Semantics.NLP.CodeBase;

            if (IsPostBack)
                return;
            try
            {
                Semantics.NLP.Initialize();
                NLP.Dictionary.InsertWord("与时俱进", "变动");
            }
            catch (Exception exp)
            {
                string excep = "Parsing Exception:" + exp.Message;
                Response.Write("<script>alert('" + excep + "')</script>");
            }
        }

        protected void ButtonParse_Click(object sender, EventArgs e)
        {

            try
            {

                SortedList<string, string> leafNode = new SortedList<string, string>();
                ISemanticParser parser = NLP.CreateSemanticParser();
                langnodes = parser.Parse(this.TextBoxInput.Text);
    
                string rootStr = "";
                TreeNode rootNode = new TreeNode(rootStr);
                //rootNode.Value = "-1";
                //this.TreeView1.Nodes.Add(rootNode);

                for (int i = 0; i < langnodes.Count; i++)
                {
                    Hash(leafNode, dictionary, null, langnodes[i]);
                    //ShowLangNode(langnodes[i], rootNode, 0);
                }
                Session["dictionary"] = dictionary;
                Session["langnodes"] = langnodes;
                foreach (KeyValuePair<string, string> item in leafNode)
                {
                    if (dictionary[item.Value].Values.Count > 1)
                    {
                        ShowInterface(item.Value);
                        break;
                    }
                }
                // this.ButtonParse.Enabled = false;
            }
            catch (Exception exp)
            {
                //this.LabelError.Text="Parsing Exception:" + exp.ToString();
                string excep = "Parsing Exception:" + exp.Message;
                Response.Write("<script>alert('" + excep + "')</script>");
            }

        }
        //在treeView控件中显示节点
        private void ShowLangNode(LanguageNode langNode, TreeNode parentNode, int level)
        {
            TreeNode childNode = new TreeNode(langNode.Value);
            if (level == 0)
            {

                this.TreeView1.Nodes.Add(childNode);
            }
            else
            {
                parentNode.ChildNodes.Add(childNode);
            }
            int count = langNode.Children.Count;
            if (count > 0)
                for (int i = 0; i < count; i++)
                {
                    LanguageNode grandChildNode = (LanguageNode)langNode.Children[i];
                    ShowLangNode(grandChildNode, childNode, level + 1);
                }
        }

        void Hash(SortedList<string, string> leafNode, SortedList<string, SortedList<string, LanguageNode>> dictionary,
            LanguageNode parentnode, LanguageNode node)
        {
            SortedList<string, LanguageNode> parents = null;
            if (!dictionary.ContainsKey(node.Value))
            {
                parents = new SortedList<string, LanguageNode>();
                dictionary.Add(node.Value, parents);
            }
            else
                parents = dictionary[node.Value];
            if (parentnode != null &&
                !parents.ContainsKey(parentnode.Value))
                parents.Add(parentnode.Value, parentnode);
            if (node.Children.Count == 0)
            {
                if (!leafNode.ContainsKey(node.Value))
                    leafNode.Add(node.Value, node.Value);
            }
            else
                for (int i = 0; i < node.Children.Count; i++)
                {
                    Hash(leafNode, dictionary, node, (LanguageNode)node.Children[i]);
                }
        }

        protected void ButtonSubmit_Click(object sender, EventArgs e)
        {
            dictionary = (SortedList<string, SortedList<string, LanguageNode>>)Session["dictionary"];
            string rootStr = "";
            TreeNode rootNode = new TreeNode(rootStr);

            int selectIndex = this.DropDownList1.SelectedIndex;
            //string a = this.DropDownList1.SelectedValue;
            string praseNode = (string)Session["PraseNode"];

            //找到选中的节点在dictionary的位置，然后查找其祖先结点，如果能找到唯一的祖先结点、则停止与用户交互选择，否则，进入从leafNode进入下一轮的用户交互。
            string key;
            key = dictionary[praseNode].Values[selectIndex].Value;
            if (dictionary[key].Count == 0)
            {
                this.TreeView1.Nodes.Clear();
                this.question.Visible = false;
                this.DropDownList1.Visible = false;
                this.ButtonSubmit.Visible = false;
                ShowLangNode(dictionary[praseNode].Values[selectIndex], rootNode, 0);
            }
            else
                findParent(key);

        }
        void findParent(string key)
        {
            string rootStr = "";
            TreeNode rootNode = new TreeNode(rootStr);


            //如果有多个Parent结点
            if (dictionary[key].Values.Count > 1)
                ShowInterface(key);
            //如果父亲结点只有一个并且父亲结点是根节点,显示结果
            else if (dictionary[key].Values.Count == 1 && string.Compare(dictionary[key].Values[0].Value, this.TextBoxInput.Text) == 0)
            {
                this.question.Visible = false;
                this.DropDownList1.Visible = false;
                this.ButtonSubmit.Visible = false;
                this.TreeView1.Nodes.Clear();
                ShowLangNode(dictionary[key].Values[0], rootNode, 0);
            }
            else
                findParent(dictionary[key].Values[0].Value);
        }
        //int findIndexInDictionary(string key)
        //{
        //    int index=0;
        //    foreach (KeyValuePair<string, SortedList<string, LanguageNode>> item in dictionary)
        //    {
        //        if (string.Compare(key, item.Key) == 0)
        //            break;
        //        else index++;
        //    }
        //    return index;            
        //}
        //LanguageNode findParent(string node)
        //{
        //     //foreach (KeyValuePair<string, SortedList<string, LanguageNode>> item in dictionary)
        //     //{
        //     //    if (string.Compare(item.Key, node) == 0)
        //     //        if (item.Value.Count > 0)
        //     //            foreach (KeyValuePair<string, LanguageNode> item2 in item.Value)
        //     //                findParent(item2.Key);
        //     //        //else ;
        //    LanguageNode land;
        //    return land;

        //     }
        // }
        void ShowInterface(string key)
        {
            this.TreeView1.Nodes.Clear();
            this.DropDownList1.Items.Clear();

            string question = "请选出" + key + "分析正确的一组:";
            this.question.Visible = true;
            this.question.Text = question;

            this.DropDownList1.Visible = true;

            Session["PraseNode"] = key;
            string selectList;
            for (int j = 0; j < dictionary[key].Values.Count; j++)
            {
                selectList = "选项" + j;
                //下拉框的选项的赋值为 该结点的父节点value
                this.DropDownList1.Items.Add(new ListItem(selectList, dictionary[key].Values[j].Value));

                //下拉框的选项的赋值为 当前结点的value
                //this.DropDownList1.Items.Add(new ListItem(selectList, key));

                //在TreeView中标记是那个选项
                TreeNode selectNode = new TreeNode(selectList);
                this.TreeView1.Nodes.Add(selectNode);

                //selectNode.ShowCheckBox = true;
                ShowLangNode(dictionary[key].Values[j], selectNode, 1);
            }
            this.ButtonSubmit.Visible = true;
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
